import Vue from "vue";
import Router from "vue-router";
import store from "../store/index";

Vue.use(Router);

const router = new Router({
  routes: [{
      path: "/",
      redirect: "/index"
    },
    {
      path: "/summary",
      meta: {
        title: "汇总"
      },
      component: () => import("@/views/summary/index")
    },
    {
      path: "/login",
      component: () => import("@/views/login/Login"),
      meta: {
        title: "登陆"
      }
    },
    {
      path: "/amendpassword",
      component: () => import("@/views/index/amendPassword"),
      meta: {
        title: "修改密码"
      }
    },
    {
      path: "/amendpersonal",
      component: () => import("@/views/index/amendPersonal"),
      meta: {
        title: "修改个人信息"
      }
    },
    {
      path: "/my",
      component: () => import("@/views/my/my"),
      meta: {
        title: "我的"
      }
      // 注册(子路由)
    },
    {
      path: "/Enroll",
      component: () => import("@/views/login/Enroll.vue"),
      meta: {
        title: '注册'
      }
    },
    {
      path: "/index",
      component: () => import("@/views/index/index"),
      meta: {
        title: '首页',
        needLogin: true,
      },
    },
    {
      path: '/audit',
      component: () => import('@/views/audit/audit'),
      meta: {
        title: '审核'
      }
    },
    {
      path: '/details',
      component: () => import('@/views/audit/details'),
      meta: {
        title: '审核详情'
      }
    },
    {
      path: "/my",
      component: () => import("@/views/my/my"),
      meta: {
        title: "我的"
      }
    },
    {
      path: "/setting",
      name: "setting",
      meta: {
        title: "设置"
      },
      component: () => import("@/views/my/setting")
    }
  ]
});

router.beforeEach((to, from, next) => {
  let {
    title,
    needLogin
  } = to.meta;
  let {
    isLogin
  } = store.state;
  document.title = title;

  if (needLogin && !isLogin) {
    next({
      path: "/login"
    });
  } else {
    next();
  }
});

export default router;